## Sid Meier's Civilization 4
## Copyright Firaxis Games 2005
##
##  AlterReligion by Bhruic
##	Rewritten by the ViSa Team

from CvPythonExtensions import *
import CvUtil
import PyHelpers
import Popup as PyPopup
import sys

#######################################################
############### COEXIST UNITSTATS MOD #################
#######################################################
import UnitStatisticsUtils
from UnitStatisticsDefines import *

objUnitStatisticsUtils = UnitStatisticsUtils.UnitStatisticsUtils()
#######################################################
############### COEXIST UNITSTATS MOD #################
#######################################################

# globals
gc = CyGlobalContext()

class SettlerReligion:
	def __init__(self):
		self.pCity = None
		
	def doUnitBuilt(self, argsList):
		pCity = argsList[0]
		pUnit = argsList[1]

		# edit by mrgenie
		# if (unit.getUnitType() != gc.getInfoTypeForString("UNIT_SETTLER")):
		#

		if (pUnit.getUnitClassType() != gc.getInfoTypeForString("UNITCLASS_SETTLER")):
			return
		listReligions = []
		for iReligion in range(gc.getNumReligionInfos()):
			if pCity.isHasReligion(iReligion):
				listReligions.append(iReligion)
		if (len(listReligions) == 0):
			pUnit.setScriptData("NO_RELIGION")
		else:
			pPlayer = gc.getPlayer(pUnit.getOwner())
			if (pPlayer.getCivics(gc.getInfoTypeForString("CIVICOPTION_RELIGION")) == gc.getInfoTypeForString("CIVIC_THEOCRACY")):
				bHasStateReligion = false
				for iList in listReligions:
					if (iList == pPlayer.getStateReligion()):
						bHasStateReligion = true
						break
				if (bHasStateReligion):
					pUnit.setScriptData(gc.getReligionInfo(pPlayer.getStateReligion()).getType())
				else:
					pUnit.setScriptData("NO_RELIGION")
			else:
				dice = gc.getGame().getMapRand()
				iRandReligion = dice.get(len(listReligions), "Religion Selection - AlterReligion PYTHON")
				iSelectedReligion = listReligions[iRandReligion]
				pUnit.setScriptData(gc.getReligionInfo(iSelectedReligion).getType())

		#######################################################
		############### COEXIST UNITSTATS MOD #################
		#######################################################
		objUnitStatisticsUtils.setupUnitStats(pUnit)
		SdToolKitAdvanced.sdObjectSetVal("UnitStats", pUnit, STARTTURN, 6000)
		#######################################################
		############### COEXIST UNITSTATS MOD #################
		#######################################################

	def doUnitLost(self, argsList):
		pUnit = argsList[0]

		# edit by mrgenie
		# if (unit.getUnitType() != gc.getInfoTypeForString("UNIT_SETTLER")):
		#
		if (pUnit.getUnitClassType() != gc.getInfoTypeForString("UNITCLASS_SETTLER")):
			return
		if (self.pCity):
			iReligion = gc.getInfoTypeForString(pUnit.getScriptData())
			if (iReligion > -1):
				self.pCity.setHasReligion(iReligion, true, true, true)
			self.pCity = None

	def doCityBuilt(self, argsList):
		pCity = argsList[0]
		self.pCity = pCity
		
